﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AnalysisServices;
using System.Data;

namespace DataSourceExplorer
{
    class Program
    {
        static void Main(string[] args)
        {
            ListDataSources();
        }
        
        // TOHLE JAKO PROVIDER DATA TEMPLATES...
        // vygenerovat EF/LINQ2SQL model a pak na něj Dynamic Data Templates napojit...

        public static void ListDataSources()
        {
            using (Server server = new Server())
            {
                server.Connect(@"data source=sednt1020");

                Database db = server.Databases["TIW_COR"];

                //foreach (Cube cube in db.Cubes)
                //{
                //    DataSource ds = cube.DataSource;
                //    DataSourceView dsv = cube.DataSourceView;
                //}

                // vyberu jednu kostku
                Cube cube = db.Cubes["coromant_Sales"];

                DataSourceView dsv = cube.DataSourceView;
                DataSet dsvSchema = dsv.Schema;
                
                // DSV schema. Je to ulozene v DataSetu.
                var dsvSchemaTables = dsvSchema.Tables;
                foreach (DataTable table in dsvSchemaTables)
                {
                    Console.WriteLine(Environment.NewLine);
                    Console.WriteLine(Environment.NewLine);
                    Console.WriteLine(table.TableName);

                    foreach (DataColumn col in table.Columns)
	                {
                        Console.WriteLine(col.ColumnName);
	                } 
                }

                Console.WriteLine(Environment.NewLine);
                Console.WriteLine(Environment.NewLine);


                // relace mezi tabulkami DSV
                foreach (DataRelation rel in dsvSchema.Relations)
	            {
		            //rel.ParentTable.TableName
                    //rel.ChildTable.TableName
	            } 

                CubeDimension pc = cube.Dimensions["Product Classification"];

                foreach (CubeHierarchy h in pc.Hierarchies)
	            {
		            //hierarchie v dimenzi PC
	            } 

                // prehled atributu dimenze PC
                foreach (CubeAttribute att in pc.Attributes)
                {
                    Binding nc = att.Attribute.NameColumn.Source;
                    
                    if (nc is ColumnBinding)
                    {
                        Console.WriteLine(((ColumnBinding)nc).TableID + "   " + ((ColumnBinding)nc).ColumnID);
                    }
                    else
                    {
                        Console.WriteLine(nc.GetType().FullName);
                    }
                    
                }
                               
                
                Console.WriteLine("Press any key...");

                Console.ReadKey(false);

            }
        }
    }
}
